package lexicalAnalysis;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import elements.Vocabulary;
/**
 * Verifica daca cuvintele sunt scrise bine si daca exista cuvinte ce nu sunt cuprinse
 * in vocabularul limbajului.
 */
public class LexicalAnalysis {
	protected Vocabulary voc;
	protected int index;

	public LexicalAnalysis() throws IOException {
		voc = new Vocabulary();
	}

	public boolean verifyBelonging(String[] words, int index)
			throws LexicalException, IOException {
		int j = 0, i;
		for (i = 0; i < words.length; i++) {
			if (voc.getActiuni().contains(words[i])){
				j++;
			}
			else if (voc.getAparate().contains(words[i])){
				j++;
			}
			else if (voc.getContext().contains(words[i])){
				j++;
			}
			else if (voc.getCorp().contains(words[i])){
				j++;
			}
			else if (voc.getIncaperi().contains(words[i])){
				j++;
			}
			else if (voc.getInincaperi().contains(words[i])){
				j++;
			}
			else if (voc.getIntensitate().contains(words[i])){
				j++;
			}
			else if (voc.getSetari().contains(words[i])){
				j++;
			}
			else if (voc.getSpalare().contains(words[i])){
				j++;
			}
			else if (voc.getStatice().contains(words[i])){
				j++;
			}
			else if (voc.getVreme().contains(words[i])){
				j++;
			}
			else if (voc.getTimp().contains(words[i])){
				j++;
			}
			else if (voc.getName().contains(words[i])){
				j++;
			}
			else if (verifyHour(words[i]) == true){
				j++;
			}
		}
		if (j != i)
			throw new LexicalException("Word not found at line " + (index+5) + " : " + words[j]+"\n");
		return false;
	}

	public boolean verifyHour(String word) throws LexicalException {
		Pattern pattern;
		Matcher matcher;
		String TIME12HOURS_PATTERN = "([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]";
		pattern = Pattern.compile(TIME12HOURS_PATTERN);
		matcher = pattern.matcher(word);
		return matcher.matches();
	}
}
